
//
//  MCFileManage.m
//  ShanDianXiu
//
//  Created by delicious on 2017/12/28.
//  Copyright © 2017年 delicious. All rights reserved.
//

#import "MCFileManager.h"
@implementation MCFileManager

/* <判断文件是否已经在沙盒文件夹中已经存在 > */
+(BOOL)isFileExist:(NSString *)fileName pathType:(pathType)Type
{
    NSString *path;
    if (Type==pathDocumentType) {
        path =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
     path =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
     path =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
    path =[NSString cachesFolder];
    }

    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:filePath];
    
    NSLog(@"这个文件已经存在：%@",result?@"是的":@"不存在");
    return result;
}

/* < 在沙盒文件中全部文件 > */
+(void)deletableisFileJiaName:(NSString *)fileName pathType:(pathType)Type
{
    NSString *path;
    if (Type==pathDocumentType) {
        path =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
        path =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
        path =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
        path =[NSString cachesFolder];
    }
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *newPath;
    if (fileName.length>0) {
        newPath = [path stringByAppendingPathComponent:fileName];
    }
    else
    {
        newPath = path;
    }
    
    
    //移除所有文件
    NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:newPath];
    for (NSString *file in enumerator) {
        [[NSFileManager defaultManager] removeItemAtPath:[newPath stringByAppendingPathComponent:file] error:nil];
    }
    
}



/* <判断文件夹中已经存在 > */
+(BOOL)isFileJiaName:(NSString *)fileName pathType:(pathType)Type
{
    NSString *path;
    if (Type==pathDocumentType) {
        path =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
        path =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
        path =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
        path =[NSString cachesFolder];
    }
    
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir                         = FALSE;
    BOOL isDirExist                    = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    NSLog(@"这个文件已经存在：%@",(isDirExist && isDir)?@"是的":@"不存在");
    return (isDirExist && isDir);
}

/* < 在沙盒文件中创建文件夹 > */
+(NSString *)createFileJiaName:(NSString *)fileName pathType:(pathType)Type
{
    NSString *FilePath;
    if (Type==pathDocumentType) {
        FilePath =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
        FilePath =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
        FilePath =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
        FilePath =[NSString cachesFolder];
    }
    NSFileManager *fileManager         = [NSFileManager defaultManager];
    
    NSString *newPath;
    if (fileName.length>0) {
        newPath = [FilePath stringByAppendingPathComponent:fileName];
    }
    else
    {
        newPath = newPath;
    }
    BOOL isDir                         = FALSE;
    BOOL isDirExist                    = [fileManager fileExistsAtPath:newPath isDirectory:&isDir];
    if(!(isDirExist && isDir))    {
        BOOL bCreateDir                = [fileManager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            NSLog(@"创建失败！");
            return @"";
        }
        NSLog(@"创建目录 FilePath%@",newPath);
    }
    return newPath;
}
+(NSString *)createFileName:(NSString *)fileName pathType:(pathType)Type
{
    NSString *FilePath;
    if (Type==pathDocumentType) {
        FilePath =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
        FilePath =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
        FilePath =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
        FilePath =[NSString cachesFolder];
    }
    
    FilePath = [FilePath stringByAppendingPathComponent:fileName];
    
    NSFileManager *fileManager         = [NSFileManager defaultManager];
    BOOL isDir                         = FALSE;
    BOOL isDirExist                    = [fileManager fileExistsAtPath:FilePath isDirectory:&isDir];
    if(!(isDirExist && isDir))    {
        
        BOOL bCreateDir                = [fileManager createFileAtPath:FilePath contents:nil attributes:nil];
        if(!bCreateDir){
            NSLog(@"创建失败！");
            return @"";
        }
        NSLog(@"创建目录 FilePath%@",FilePath);
    }
    return FilePath;
}

+(BOOL)deletableFileName:(NSString *)fileName pathType:(pathType)Type
{
    NSString *FilePath;
    if (Type==pathDocumentType) {
        FilePath =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
        FilePath =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
        FilePath =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
        FilePath =[NSString cachesFolder];
    }
    
    FilePath = [FilePath stringByAppendingPathComponent:fileName];
    
    NSFileManager *fileManager         = [NSFileManager defaultManager];
    BOOL isDir                         = FALSE;
    BOOL isDirExist                    = [fileManager fileExistsAtPath:FilePath isDirectory:&isDir];
    if(isDirExist)    {
        
        BOOL bCreateDir                = [fileManager removeItemAtPath:FilePath error:nil];
        if(!bCreateDir){
            NSLog(@"删除失败！");
            return bCreateDir;
        }
        NSLog(@"删除成功！");
            return bCreateDir;
    }
    return isDir;
}

/* < 把对象归档存到沙盒里 > */
+ (BOOL)saveObject:(id)object byFileName:(NSString *)fileName pathType:(pathType)Type{
    NSString *path;
    if (Type==pathDocumentType) {
        path =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
        path =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
        path =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
        path =[NSString cachesFolder];
    }
    if (fileName) {
        path = [path stringByAppendingPathComponent:fileName];
    }
    path = [path stringByAppendingString:@".archive"];
    BOOL success = [NSKeyedArchiver archiveRootObject:object toFile:path];
    return success;
    
}

/* < 通过文件名从沙盒中找到归档的对象 > */
+ (id)getObjectByFileName:(NSString*)fileName  pathType:(pathType)Type{
    NSString *path;
    if (Type==pathDocumentType) {
        path =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
        path =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
        path =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
        path =[NSString cachesFolder];
    }
    path = [path stringByAppendingString:@".archive"];
    id obj =  [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return obj;
}
/* < 在沙盒文件中删除归档的对象 > */
+(BOOL)deletableForFileName:(NSString *)fileName pathType:(pathType)Type
{
    fileName = [fileName stringByAppendingString:@".archive"];
    return [self deletableFileName:fileName pathType:Type];
}

+ (long long)getSizeByFileName:(NSString*)fileName  pathType:(pathType)Type
{
    NSString *path;
    if (Type==pathDocumentType) {
        path =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
        path =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
        path =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
        path =[NSString cachesFolder];
    }
    
    NSFileManager* manager   = [NSFileManager defaultManager];
  
    if (fileName) {
        path = [path stringByAppendingPathComponent:fileName];
    }
    if ([manager fileExistsAtPath:path]){
        
        return [[manager attributesOfItemAtPath:path error:nil] fileSize];
    }
    
    return 0;
}


+ (float)getFolderSizeByFileName:(NSString*)fileName  pathType:(pathType)Type{
    
    NSString *path;
    if (Type==pathDocumentType) {
        path =[NSString documentFolder];
    }
    else if (Type==pathLibraryType)
    {
        path =[NSString libraryFolder];
    }
    else if (Type==pathTmpType)
    {
        path =[NSString tmpFolder];
        
    }else if (Type==pathCachesType)
    {
        path =[NSString cachesFolder];
    }
    
    NSFileManager* manager   = [NSFileManager defaultManager];
    
    if (fileName) {
        path = [path stringByAppendingPathComponent:fileName];
    }

    if (![manager fileExistsAtPath:path]) return 0;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:path] objectEnumerator];
    
    NSString* file;
    
    long long folderSize               = 0;
    
    while ((file = [childFilesEnumerator nextObject]) != nil){
        
        NSString* fileAbsolutePath         = [path stringByAppendingPathComponent:file];
        
        if ([manager fileExistsAtPath:fileAbsolutePath]){
            
           folderSize  +=  [[manager attributesOfItemAtPath:fileAbsolutePath error:nil] fileSize];
        }
    }
    
    return folderSize/(1024.0*1024.0);
}


@end


@implementation NSString (path)

+(NSString *)homeFolder
{
      return NSHomeDirectory();
}
/*
 *  document根文件夹
 */
+(NSString *)documentFolder{
    
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}



/*
 *  tmp根文件夹
 */
+(NSString *)tmpFolder{
    
    return  NSTemporaryDirectory();
}

/*
 *  library根文件夹
 */
+(NSString *)libraryFolder {
    return NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
}

/*
 *  caches根文件夹
 */
+(NSString *)cachesFolder{
    
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
}

/**
 *  生成子文件夹
 *
 *  如果子文件夹不存在，则直接创建；如果已经存在，则直接返回
 *
 *  @param subFolder 子文件夹名
 *
 *  @return 文件夹路径
 */
-(NSString *)createSubFolder:(NSString *)subFolder{
    
    NSString *subFolderPath=[NSString stringWithFormat:@"%@/%@",self,subFolder];
    
    BOOL isDir = NO;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL existed = [fileManager fileExistsAtPath:subFolderPath isDirectory:&isDir];
    
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:subFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return subFolderPath;
}

+(NSString *)onPathByOtherPath:(NSString *)otherPath{
    
    NSString *sourcePath =[self documentFolder];
    
    NSString *path = [sourcePath stringByAppendingString:otherPath];
    sourcePath = nil;
    otherPath = nil;
    return path?path:nil;
}




@end
